package cn.st4rlight.util.excel.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;

import cn.st4rlight.util.time.DateTimes;

/**
 * 用于easy-excel的 yyyyMMdd --> yyyy-MM-dd
 * @author st4rlight <st4rlight@163.com>
 * Created on 2025-05-03
 */
public class PDataDashConverter implements Converter<String> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<String> context) {
        String value = context.getValue();
        if (!DateTimes.isValidDateFormat(value, DateTimes.DATE_PATTERN_NO_DASH)) {
            new WriteCellData<>(CellDataTypeEnum.STRING, "-");
        }
        String resultStr = DateTimes.from(value, DateTimes.DATE_PATTERN_NO_DASH).formatter(DateTimes.DATE_PATTERN).toString();
        return new WriteCellData<>(CellDataTypeEnum.STRING, resultStr);
    }
}
